<template>
  <div id="drawerPlus">
    <el-drawer
      ref="drawer"
      :visible.sync="show"
      v-bind="$attrs"
      custom-class="demo-drawer"
    >
      <el-scrollbar class="scrollbar">
        <slot />
        <div class="seat"></div>
        <div class="footer">
          <slot name="footer">
            <template>
              <el-button :disabled="loading" @click="cancelForm"
                >取消</el-button
              >
              <el-button type="primary" :loading="loading" @click="submit">{{
                loading ? "提交中 ..." : "确 定"
              }}</el-button>
            </template>
          </slot>
        </div>
      </el-scrollbar>
    </el-drawer>
  </div>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: () => false,
    },
  },
  data() {
    return {
      show: false,
      loading: false,
      timer: null,
    };
  },
  watch: {
    show: function (newVal) {
      this.$emit("update:visible", newVal);
    },
    visible: function (val) {
      this.show = val;
    },
  },
  methods: {
    submit() {
      if (this.loading) return;
      this.$confirm("确定要提交表单吗？")
        .then(() => {
          this.onLoading();
          this.$emit(
            "on-submit",
            () => {
              this.cancelForm();
            },
            () => {
              this.endLoading();
            }
          );
        })
        .catch(() => {
          this.endLoading();
        });
    },
    /** 加载时确定按钮文字和状态 */
    onLoading() {
      this.loading = true;
    },

    /** 完成加载后得确认按钮文字和状态 */
    endLoading() {
      this.loading = false;
    },

    /** 关闭 */
    cancelForm() {
      this.loading = false;
      this.show = false;
      this.$emit("on-close");
    },
  },
};
</script>

<style lang="less" scoped>
@height: 100px;
#drawerPlus {
  .scrollbar {
    height: 100%;
    position: relative;
    .seat {
      height: @height;
    }
    .footer {
      z-index: 9;
      box-shadow: 0 -4px 6px rgba(0, 0, 0, 0.08);
      width: 100%;
      position: absolute;
      bottom: 0px;
      height: @height;
      background-color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}
</style>